home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / gp / unix.c < prev    next >
C/C++ Source or Header  |  1991-12-16  |  6KB  |  222 lines

  1. /*--------------------------------*\
  2. | Additions for UNIX-compatibility |
  3. \*--------------------------------*/
  4. #ifdef __unix__
  5.  
  6. /*#ifdef __vax__
  7. char *strdup(s1)
  8.   char *s1;
  9. { char *s;
  10.  
  11.   s = malloc(strlen(s1)+1);
  12.   strcpy(s,s1);
  13.   return(s);
  14. }
  15. #endif /* __vax */
  16.  
  17. static int first = 1;
  18.  
  19. int my_getch()
  20. { unsigned char c;
  21.   int fd;
  22.  
  23.   fd = fileno (stdin);
  24.   if (first) {
  25.     first = 0;
  26. #ifdef SYSV
  27.     (void) ioctl(fd, TCGETA, (char *) &sg[OFF]);
  28. #else
  29.     (void) gtty(fd, &sg[OFF]);
  30. #endif
  31.     sg[ON] = sg[OFF];
  32.  
  33. #ifdef SYSV
  34.     sg[ON].c_lflag &= ~(ICANON|ECHO);
  35.     sg[ON].c_cc[VMIN] = 1;
  36.     sg[ON].c_cc[VTIME] = 0;
  37. #else
  38.     sg[ON].sg_flags &= ~(ECHO | CRMOD);
  39.     sg[ON].sg_flags |= CBREAK;
  40. #endif
  41.   }
  42.  
  43. #ifdef SYSV
  44.   (void) ioctl(fd, TCSETAW, (char *) &sg[ON]);
  45. #else
  46.   (void) stty(fd, &sg[ON]);
  47. #endif
  48.  
  49.   read(fd, &c, 1);
  50.  
  51. #ifdef SYSV
  52.   (void) ioctl(fd, TCSETAW, (char *) &sg[OFF]);
  53. #else
  54.   (void) stty(fd, &sg[OFF]);
  55. #endif
  56.  
  57.   return (int) c;
  58. }
  59.  
  60. #ifdef __i386__
  61. #define MAXCMD 1024
  62.  
  63. int rename(char *s1, char *s2)
  64. {
  65.   char tmp[MAXCMD];
  66.  
  67.   (void) sprintf(tmp, "mv %s %s", s1, s2);
  68.   (void) system(tmp);
  69. }
  70.  
  71. /*
  72.   strstr - public-domain implementation of standard C library function
  73.  
  74.   last edit:  02-Sep-1990  D A Gwyn
  75.  
  76.   This is an original implementation based on an idea by D M Sunday,
  77.   essentially the "quick search" algorithm described in CACM V33 N8.
  78.   Unlike Sunday's implementation, this one does not wander past the
  79.   ends of the strings (which can cause malfunctions under certain
  80.   circumstances), nor does it require the length of the searched
  81.   text to be determined in advance.  There are numerous other subtle
  82.   improvements too.  The code is intended to be fully portable, but in
  83.   environments that do not conform to the C standard, you should check
  84.   the sections below marked "configure as required".  There are also
  85.   a few compilation options, as follows:
  86.  
  87.   #define ROBUST  to obtain sane behavior when invoked with a null
  88.       pointer argument, at a miniscule cost in speed
  89.   #define ZAP  to use memset() to zero the shift[] array; this may
  90.       be faster in some implementations, but could fail on
  91.       unusual architectures
  92.   #define DEBUG  to enable assertions (bug detection)
  93. */
  94. #define ROBUST
  95. #define ZAP
  96.  
  97. #ifdef __STDC__
  98.  
  99. #include  <stddef.h>    /* defines size_t and NULL */
  100. #include  <limits.h>    /* defines UCHAR_MAX */
  101.  
  102. #ifdef ZAP
  103. typedef void  *pointer;
  104. extern pointer  memset( pointer, int, size_t );
  105. #endif
  106.  
  107. #else  /* normal UNIX-like C environment assumed; configure as required: */
  108.  
  109. typedef unsigned  size_t;  /* type of result of sizeof */
  110.  
  111. #ifndef NULL
  112. #define  NULL    0    /* null pointer constant */
  113. #endif
  114.  
  115. #define  UCHAR_MAX  255    /* largest value of unsigned char */
  116.           /* 255 @ 8 bits, 65535 @ 16 bits */
  117.  
  118. #ifdef ZAP
  119. typedef char  *pointer;
  120. extern pointer  memset();
  121. #endif
  122.  
  123. #define const  /* nothing */
  124.  
  125. #endif  /* __STDC__ */
  126.  
  127. #ifndef DEBUG
  128. #define  NDEBUG
  129. #endif
  130. #include  <assert.h>
  131.  
  132. typedef const unsigned char  cuc;  /* char variety used in algorithm */
  133.  
  134. #define EOS  '\0'      /* C string terminator */
  135.  
  136. char *          /* returns -> leftmost occurrence,
  137.                    or null pointer if not present */
  138. strstr( s1, s2 )
  139.   const char  *s1;          /* -> string to be searched */
  140.   const char  *s2;    /* -> search-pattern string */
  141.   {
  142.   register cuc  *t;      /* -> text character being tested */
  143.   register cuc  *p;      /* -> pattern char being tested */
  144.   register cuc  *tx;    /* -> possible start of match */
  145.   register size_t  m;    /* length of pattern */
  146.   register cuc  *top;    /* -> high water mark in text */
  147. #if UCHAR_MAX > 255      /* too large for auto allocation */
  148.   static        /* not malloc()ed; that can fail! */
  149. #endif          /* else allocate shift[] on stack */
  150.     size_t  shift[UCHAR_MAX + 1];  /* pattern shift table */
  151.  
  152. #ifdef ROBUST        /* not required by C standard */
  153.   if ( s1 == NULL || s2 == NULL )
  154.     return NULL;    /* certainly, no match is found! */
  155. #endif
  156.  
  157.   /* Precompute shift intervals based on the pattern;
  158.      the length of the pattern is determined as a side effect: */
  159.  
  160. #ifdef ZAP
  161.   (void)memset( (pointer)&shift[1], 0, UCHAR_MAX * sizeof(size_t) );
  162. #else
  163.   {
  164.   register unsigned char  c;
  165.  
  166.   c = UCHAR_MAX;
  167.   do
  168.     shift[c] = 0;
  169.   while ( --c > 0 );
  170.   }
  171. #endif /* ZAP */
  172.       /* Note: shift[0] is undefined at this point (fixed later). */
  173.  
  174.   for ( m = 1, p = (cuc *)s2; *p != EOS; ++m, ++p )
  175.     shift[(cuc)*p] = m;
  176.  
  177.   assert(s2[m - 1] == EOS);
  178.  
  179.   {
  180.   register unsigned char  c;
  181.  
  182.   c = UCHAR_MAX;
  183.   do
  184.     shift[c] = m - shift[c];
  185.   while ( --c > 0 );
  186.  
  187.   /* Note: shift[0] is still undefined at this point. */
  188.   }
  189.  
  190.   shift[0] = --m;    /* shift[EOS]; important details! */
  191.  
  192.   assert(s2[m] == EOS);
  193.  
  194.   /* Try to find the pattern in the text string: */
  195.  
  196.   for ( top = tx = (cuc *)s1; ; tx += shift[*(top = t)] )
  197.     {
  198.     for ( t = tx, p = (cuc *)s2; ; ++t, ++p )
  199.       {
  200.       if ( *p == EOS )       /* entire pattern matched */
  201.         return (char *)tx;
  202.  
  203.       if ( *p != *t )
  204.         break;
  205.       }
  206.  
  207.     if ( t < top )    /* idea due to ado@elsie.nci.nih.gov */
  208.       t = top;  /* already scanned this far for EOS */
  209.  
  210.     do  {
  211.       assert(m > 0);
  212.       assert(t - tx < m);
  213.  
  214.       if ( *t == EOS )
  215.         return NULL;  /* no match */
  216.       }
  217.     while ( ++t - tx != m );  /* < */
  218.     }
  219.   }
  220. #endif
  221. #endif /* __unix__ */
  222.